package com.s.core.daily_import;

import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

@Slf4j
public class Utils {
    public static String time() {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return LocalDateTime.now().format(formatter);
    }

    public static String tranDate(String dateString, String originalFmt, String targetFmt) {
        SimpleDateFormat originalFormat = new SimpleDateFormat(originalFmt);
        SimpleDateFormat targetFormat = new SimpleDateFormat(targetFmt);
        Date date;
        try {
            date = originalFormat.parse(dateString);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        return targetFormat.format(date);
    }

    public static String toMonth(String date) {
        LocalDate tmpDate = LocalDate.parse(date, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");
        return tmpDate.format(formatter);
    }

    public static String nextMonth(String month) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");
        LocalDate currentDate = LocalDate.parse(month + "-01", DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        LocalDate nextMonth = currentDate.plusMonths(1);
        return nextMonth.format(formatter);
    }

    public static String fen2yuan(int yuan) {
        BigDecimal b = new BigDecimal(String.valueOf(yuan)).divide(new BigDecimal(100));
        if (b.scale() == 0) {
            b = b.setScale(1, RoundingMode.HALF_UP);
        }
        return b.toString();
    }

    public static String httpGet(String u) {
        HttpURLConnection conn = null;
        BufferedReader reader = null;
        String rs = null;
        try {
            URL url = new URL(u);
            conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setUseCaches(false);
            conn.setConnectTimeout(3000);
            conn.setReadTimeout(3000);
            conn.setInstanceFollowRedirects(false);
//            conn.setRequestProperty("referer", "https://finance.sina.com.cn");
            conn.connect();

            StringBuilder sb = new StringBuilder();
            String strRead;
            reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8));
            while ((strRead = reader.readLine()) != null) {
                sb.append(strRead);
            }
            rs = sb.toString();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                }
            }
            if (conn != null) {
                conn.disconnect();
            }
        }
        return rs;
    }
}